﻿Public Module PublicFunction
    ''' <summary>
    ''' 采用距离倒数乘方法计算任意点背景值
    ''' </summary>
    ''' <param name="DisCare">监测的关心点的坐标的数组</param>
    ''' <param name="DisCon">监测的关心点的坐标的对应的浓度的数组</param>
    ''' <param name="n">次方</param>
    ''' <remarks></remarks>
    Public Function GetLocationCon(ByVal disPoint3D As Point3D, ByVal DisCare() As Point3D, ByVal DisCon() As Double, Optional ByVal n As Double = 2) As Double
        If DisCare.Length = 0 Then
            Return 0
        End If
        Dim D As Double = 0 '距离的乘方倒数之和
        Dim DB As Double = 0
        For i As Integer = 0 To DisCare.Length - 1
            Dim Distance As Double = PublicFunction.GetDistane3D(disPoint3D, DisCare(i))
            If Distance <= 0 Then
                D = 0
                Return DisCon(i)
                Exit For
            Else
                Dim Di As Double = 1 / Distance ^ n
                D += Di
            End If
        Next
        For i As Integer = 0 To DisCare.Length - 1
            Dim Distance As Double = PublicFunction.GetDistane3D(disPoint3D, DisCare(i))
            If Distance <= 0 Then
                D = 0
                Return DisCon(i)
                Exit For
            Else
                Dim Di As Double = 1 / Distance ^ n
                DB += DisCon(i) * Di / D
            End If
        Next
        Return DB
    End Function

    ''' <summary>
    ''' 计算三维空间上的两个点之间的距离
    ''' </summary>
    ''' <param name="loc1"></param>
    ''' <param name="loc2"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetDistane3D(ByVal loc1 As Point3D, ByVal loc2 As Point3D) As Double
        Dim _distance As Double

        Dim d1 As Double
        Dim d2 As Double
        Dim d3 As Double

        d1 = loc1.X - loc2.X
        d2 = loc1.Y - loc2.Y
        d3 = loc1.Z - loc2.Z

        _distance = Math.Sqrt(d1 * d1 + d2 * d2 + d3 * d3)
        Return _distance
    End Function
End Module
